<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8" />
    
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
  <title>
    实例演示axios拦截器和取消请求.md |  Small Bridge
  </title>
  <meta name="generator" content="hexo-theme-yilia-plus">
  
  <link rel="shortcut icon" href="/favicon.ico" />
  
  
<link rel="stylesheet" href="/css/main.css">

  
  <script src="https://cdn.jsdelivr.net/npm/pace-js@1.0.2/pace.min.js"></script>
  
  

  

<link rel="alternate" href="/atom.xml" title="Small Bridge" type="application/atom+xml">
</head>

</html>

<body>
  <div id="app">
    <main class="content">
      <section class="outer">
  <article id="post-实例演示axios拦截器和取消请求-md" class="article article-type-post" itemscope
  itemprop="blogPost" data-scroll-reveal>

  <div class="article-inner">
    
    <header class="article-header">
       
<h1 class="article-title sea-center" style="border-left:0" itemprop="name">
  实例演示axios拦截器和取消请求.md
</h1>
  

    </header>
    

    
    <div class="article-meta">
      <a href="/2020/09/07/%E5%AE%9E%E4%BE%8B%E6%BC%94%E7%A4%BAaxios%E6%8B%A6%E6%88%AA%E5%99%A8%E5%92%8C%E5%8F%96%E6%B6%88%E8%AF%B7%E6%B1%82-md/" class="article-date">
  <time datetime="2020-09-07T13:30:47.000Z" itemprop="datePublished">2020-09-07</time>
</a>
      
  <div class="article-category">
    <a class="article-category-link" href="/categories/JavaScript/">JavaScript</a>
  </div>

      
      
<div class="word_count">
    <span class="post-time">
        <span class="post-meta-item-icon">
            <i class="ri-quill-pen-line"></i>
            <span class="post-meta-item-text"> 字数统计:</span>
            <span class="post-count">2.3k字</span>
        </span>
    </span>

    <span class="post-time">
        &nbsp; | &nbsp;
        <span class="post-meta-item-icon">
            <i class="ri-book-open-line"></i>
            <span class="post-meta-item-text"> 阅读时长≈</span>
            <span class="post-count">11分钟</span>
        </span>
    </span>
</div>

      
    </div>
    

    
    
    <div class="tocbot"></div>





    

    
    <div class="article-entry" itemprop="articleBody">
      


      

      
      <p>@<a href="axios">TOC</a></p>
<h1 id="axios拦截器"><a href="#axios拦截器" class="headerlink" title="axios拦截器"></a>axios拦截器</h1><a id="more"></a>
<dl><dt>拦截器是什么？</dt><dd>以我的理解，拦截器就是一个回调函数，里面包含了一些逻辑处理的代码，它分为请求拦截器，其在请求发送之前执行，处理一些请求发送之前的逻辑，响应拦截器，其在响应拿到之后，回调函数执行之前执行，处理一些拿到响应数据的逻辑。</dd></dl><p>为了方便演示，我使用koa写了一个简易的接口，用于处理请求返回数据</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> Koa = <span class="built_in">require</span>(<span class="string">'koa'</span>);</span><br><span class="line"><span class="keyword">const</span> router = <span class="built_in">require</span>(<span class="string">'koa-router'</span>)();</span><br><span class="line"><span class="keyword">const</span> jsonp = <span class="built_in">require</span>(<span class="string">'koa-jsonp'</span>);</span><br><span class="line"><span class="keyword">const</span> cors = <span class="built_in">require</span>(<span class="string">'koa-cors'</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> app = <span class="keyword">new</span> Koa();</span><br><span class="line"></span><br><span class="line">app.use(cors());</span><br><span class="line">app.use(jsonp());</span><br><span class="line"></span><br><span class="line"><span class="keyword">async</span> <span class="function"><span class="keyword">function</span> <span class="title">delay</span>(<span class="params">time</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">new</span> <span class="built_in">Promise</span>(<span class="function">(<span class="params">resolve, reject</span>) =&gt;</span> &#123;</span><br><span class="line">        setTimeout(<span class="function"><span class="params">()</span> =&gt;</span> &#123;</span><br><span class="line">            resolve()</span><br><span class="line">        &#125;, time)</span><br><span class="line">    &#125;)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">router.get(<span class="string">'/products1'</span>, <span class="keyword">async</span> (ctx) =&gt; &#123;</span><br><span class="line">    <span class="keyword">await</span> delay(<span class="number">1000</span> + <span class="built_in">Math</span>.random() * <span class="number">1000</span>)</span><br><span class="line">    ctx.body = [&#123;</span><br><span class="line">            id: <span class="number">1</span>,</span><br><span class="line">            name: <span class="string">'products1.1'</span></span><br><span class="line">        &#125;,</span><br><span class="line">        &#123;</span><br><span class="line">            id: <span class="number">2</span>,</span><br><span class="line">            name: <span class="string">'products1.2'</span></span><br><span class="line">        &#125;,</span><br><span class="line">        &#123;</span><br><span class="line">            id: <span class="number">3</span>,</span><br><span class="line">            name: <span class="string">'products1.3'</span></span><br><span class="line">        &#125;</span><br><span class="line">    ];</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line">router.get(<span class="string">'/products2'</span>, <span class="keyword">async</span> (ctx) =&gt; &#123;</span><br><span class="line">    <span class="keyword">await</span> delay(<span class="number">1000</span> + <span class="built_in">Math</span>.random() * <span class="number">1000</span>)</span><br><span class="line">    ctx.body = [&#123;</span><br><span class="line">            id: <span class="number">1</span>,</span><br><span class="line">            name: <span class="string">'products2.1'</span></span><br><span class="line">        &#125;,</span><br><span class="line">        &#123;</span><br><span class="line">            id: <span class="number">2</span>,</span><br><span class="line">            name: <span class="string">'products2.2'</span></span><br><span class="line">        &#125;,</span><br><span class="line">        &#123;</span><br><span class="line">            id: <span class="number">3</span>,</span><br><span class="line">            name: <span class="string">'products2.3'</span></span><br><span class="line">        &#125;</span><br><span class="line">    ];</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line">app.use(router.routes());</span><br><span class="line">app.use(router.allowedMethods());</span><br><span class="line">app.listen(<span class="number">4000</span>);</span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(<span class="string">'Server running at http://localhost:4000'</span>);</span><br></pre></td></tr></table></figure>
<p>演示</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br></pre></td><td class="code"><pre><span class="line">&lt;!DOCTYPE html&gt;</span><br><span class="line">&lt;html lang=<span class="string">"en"</span>&gt;</span><br><span class="line"></span><br><span class="line">&lt;head&gt;</span><br><span class="line">    &lt;meta charset=<span class="string">"UTF-8"</span>&gt;</span><br><span class="line">    &lt;meta name=<span class="string">"viewport"</span> content=<span class="string">"width=device-width, initial-scale=1.0"</span>&gt;</span><br><span class="line">    &lt;title&gt;Document&lt;<span class="regexp">/title&gt;</span></span><br><span class="line"><span class="regexp">&lt;/</span>head&gt;</span><br><span class="line"></span><br><span class="line">&lt;body&gt;</span><br><span class="line">    &lt;script src=<span class="string">"https://cdn.bootcdn.net/ajax/libs/axios/0.20.0-0/axios.min.js"</span>&gt;&lt;<span class="regexp">/script&gt;</span></span><br><span class="line"><span class="regexp">    &lt;script&gt;</span></span><br><span class="line"><span class="regexp">        /</span><span class="regexp">/添加请求拦截器(回调函数)</span></span><br><span class="line"><span class="regexp">        axios.interceptors.request.use(</span></span><br><span class="line"><span class="regexp">            config =&gt; &#123;</span></span><br><span class="line"><span class="regexp">                console.log('request interceptor1 onResolved()')</span></span><br><span class="line"><span class="regexp">                return config</span></span><br><span class="line"><span class="regexp">            &#125;,</span></span><br><span class="line"><span class="regexp">            error =&gt; &#123;</span></span><br><span class="line"><span class="regexp">                console.log('request interceptor1 onRejected()')</span></span><br><span class="line"><span class="regexp">                return Promise.reject(error)</span></span><br><span class="line"><span class="regexp">            &#125;</span></span><br><span class="line"><span class="regexp">        )</span></span><br><span class="line"><span class="regexp">        axios.interceptors.request.use(</span></span><br><span class="line"><span class="regexp">            config =&gt; &#123;</span></span><br><span class="line"><span class="regexp">                console.log('request interceptor2 onResolved()')</span></span><br><span class="line"><span class="regexp">                return config</span></span><br><span class="line"><span class="regexp">            &#125;,</span></span><br><span class="line"><span class="regexp">            error =&gt; &#123;</span></span><br><span class="line"><span class="regexp">                console.log('request interceptor2 onRejected()')</span></span><br><span class="line"><span class="regexp">                return Promise.reject(error)</span></span><br><span class="line"><span class="regexp">            &#125;</span></span><br><span class="line"><span class="regexp">        )</span></span><br><span class="line"><span class="regexp">        /</span><span class="regexp">/添加响应拦截器</span></span><br><span class="line"><span class="regexp">        axios.interceptors.response.use(</span></span><br><span class="line"><span class="regexp">            response =&gt; &#123;</span></span><br><span class="line"><span class="regexp">                console.log('response interceptor1 onResolved()')</span></span><br><span class="line"><span class="regexp">                return response</span></span><br><span class="line"><span class="regexp">            &#125;,</span></span><br><span class="line"><span class="regexp">            error =&gt; &#123;</span></span><br><span class="line"><span class="regexp">                console.log('response interceptor1 onRejected()')</span></span><br><span class="line"><span class="regexp">                return Promise.reject(error)</span></span><br><span class="line"><span class="regexp">            &#125;</span></span><br><span class="line"><span class="regexp">        )</span></span><br><span class="line"><span class="regexp">        axios.interceptors.response.use(</span></span><br><span class="line"><span class="regexp">            response =&gt; &#123;</span></span><br><span class="line"><span class="regexp">                console.log('response interceptor2 onResolved()')</span></span><br><span class="line"><span class="regexp">                return response</span></span><br><span class="line"><span class="regexp">            &#125;,</span></span><br><span class="line"><span class="regexp">            error =&gt; &#123;</span></span><br><span class="line"><span class="regexp">                console.log('response interceptor2 onRejected()')</span></span><br><span class="line"><span class="regexp">                return Promise.reject(error)</span></span><br><span class="line"><span class="regexp">            &#125;</span></span><br><span class="line"><span class="regexp">        )</span></span><br><span class="line"><span class="regexp"></span></span><br><span class="line"><span class="regexp">        axios.get('http:/</span><span class="regexp">/localhost:3000/</span>posts<span class="string">')</span></span><br><span class="line"><span class="string">            .then(response =&gt; &#123;</span></span><br><span class="line"><span class="string">                console.log('</span>data<span class="string">', response.data);</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">            &#125;)</span></span><br><span class="line"><span class="string">            .catch(error =&gt; &#123;</span></span><br><span class="line"><span class="string">                console.log('</span>error<span class="string">', error.message)</span></span><br><span class="line"><span class="string">            &#125;)</span></span><br><span class="line"><span class="string">    &lt;/script&gt;</span></span><br><span class="line"><span class="string">&lt;/body&gt;</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">&lt;/html&gt;</span></span><br></pre></td></tr></table></figure>
<dl><dt>执行顺序</dt><dd>请求拦截器2 ——请求拦截器1——-发送ajax请求——-响应拦截器1——-响应拦截器2——响应回调<br>此流程是通过Promise串联起来的，请求拦截器传递的是config，响应拦截器传递的是response</dd></dl><h1 id="axios取消发送请求"><a href="#axios取消发送请求" class="headerlink" title="axios取消发送请求"></a>axios取消发送请求</h1><p>取消发送请求，顾名思义就是取消正在发送的ajax请求，这在业务场景中是会遇到的</p>
<h4 id="基本流程"><a href="#基本流程" class="headerlink" title="基本流程"></a>基本流程</h4><ol>
<li>配置cancleToken对象</li>
<li>缓存用于取消的cancel函数</li>
<li>在后面特定时机调用cancel取消请求<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br></pre></td><td class="code"><pre><span class="line">&lt;!DOCTYPE html&gt;</span><br><span class="line">&lt;html lang=<span class="string">"en"</span>&gt;</span><br><span class="line"></span><br><span class="line">&lt;head&gt;</span><br><span class="line">    &lt;meta charset=<span class="string">"UTF-8"</span>&gt;</span><br><span class="line">    &lt;meta name=<span class="string">"viewport"</span> content=<span class="string">"width=device-width, initial-scale=1.0"</span>&gt;</span><br><span class="line">    &lt;title&gt;取消请求&lt;<span class="regexp">/title&gt;</span></span><br><span class="line"><span class="regexp">&lt;/</span>head&gt;</span><br><span class="line"></span><br><span class="line">&lt;body&gt;</span><br><span class="line">    &lt;div&gt;</span><br><span class="line">        &lt;button onclick=<span class="string">"getOne()"</span>&gt;获取商品列表<span class="number">1</span>&lt;<span class="regexp">/button&gt;</span></span><br><span class="line"><span class="regexp">        &lt;button onclick="getTwo()"&gt;获取商品列表2&lt;/</span>button&gt;</span><br><span class="line">        &lt;button onclick=<span class="string">"cancelReq()"</span>&gt;取消请求&lt;<span class="regexp">/button&gt;</span></span><br><span class="line"><span class="regexp">    &lt;/</span>div&gt;</span><br><span class="line">    &lt;script src=<span class="string">"https://cdn.bootcdn.net/ajax/libs/axios/0.20.0-0/axios.min.js"</span>&gt;&lt;<span class="regexp">/script&gt;</span></span><br><span class="line"><span class="regexp">    &lt;script&gt;</span></span><br><span class="line"><span class="regexp">        let cancel /</span><span class="regexp">/用于保存取消请求的函数</span></span><br><span class="line"><span class="regexp">        function getOne() &#123;</span></span><br><span class="line"><span class="regexp">            axios(&#123;</span></span><br><span class="line"><span class="regexp">                    url: 'http:/</span><span class="regexp">/localhost:4000/</span>products1<span class="string">',</span></span><br><span class="line"><span class="string">                    cancelToken: new axios.CancelToken((c) =&gt; &#123; //c是用于取消当前请求的函数</span></span><br><span class="line"><span class="string">                        //保存取消函数，用于之后可能需要取消当前请求</span></span><br><span class="line"><span class="string">                        cancel = c</span></span><br><span class="line"><span class="string">                    &#125;)</span></span><br><span class="line"><span class="string">                &#125;)</span></span><br><span class="line"><span class="string">                .then(</span></span><br><span class="line"><span class="string">                    //不管成功还是失败，都应该没有机会取消了</span></span><br><span class="line"><span class="string">                    response =&gt; &#123;</span></span><br><span class="line"><span class="string">                        cancel = null</span></span><br><span class="line"><span class="string">                        console.log('</span>请求<span class="number">1</span>成功了<span class="string">', response.data)</span></span><br><span class="line"><span class="string">                    &#125;,</span></span><br><span class="line"><span class="string">                    error =&gt; &#123;</span></span><br><span class="line"><span class="string">                        cancel = null</span></span><br><span class="line"><span class="string">                        console.log('</span>请求<span class="number">1</span>失败了<span class="string">', error.message)</span></span><br><span class="line"><span class="string">                    &#125;</span></span><br><span class="line"><span class="string">                )</span></span><br><span class="line"><span class="string">        &#125;</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">        function getTwo() &#123;</span></span><br><span class="line"><span class="string">            axios(&#123;</span></span><br><span class="line"><span class="string">                    url: '</span>http:<span class="comment">//localhost:4000/products2',</span></span><br><span class="line">                &#125;)</span><br><span class="line">                .then(</span><br><span class="line">                    response =&gt; &#123;</span><br><span class="line">                        <span class="built_in">console</span>.log(<span class="string">'请求2成功了'</span>, response.data)</span><br><span class="line">                    &#125;,</span><br><span class="line">                    error =&gt; &#123;</span><br><span class="line">                        <span class="built_in">console</span>.log(<span class="string">'请求2失败了'</span>, error.message)</span><br><span class="line">                    &#125;</span><br><span class="line">                )</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="function"><span class="keyword">function</span> <span class="title">cancelReq</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">            <span class="comment">//alert('取消请求')</span></span><br><span class="line">            <span class="comment">//执行取消请求的函数</span></span><br><span class="line">            <span class="keyword">if</span> (<span class="keyword">typeof</span> cancel === <span class="string">'function'</span>) &#123;</span><br><span class="line">                cancel(<span class="string">'强制取消请求'</span>)</span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                <span class="built_in">console</span>.log(<span class="string">'没有可取消的请求'</span>)</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &lt;<span class="regexp">/script&gt;</span></span><br><span class="line"><span class="regexp">&lt;/</span>body&gt;</span><br><span class="line"></span><br><span class="line">&lt;<span class="regexp">/html&gt;</span></span><br></pre></td></tr></table></figure>
<h1 id="实例演示"><a href="#实例演示" class="headerlink" title="实例演示"></a>实例演示</h1>上面的那个例子实现了点击按钮取消发送请求，但是我们会遇到一个情况，用户点击获取列表1的数据之后又点击获取列表2的数据，若列表1和列表2与前台同一位置显示，那么先前请求的列表1的数据的请求是否就可以直接中断而光去请求列表2的数据，这就需要实现在请求一个接口前，取消前面一个未完成的请求<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br></pre></td><td class="code"><pre><span class="line">&lt;!DOCTYPE html&gt;</span><br><span class="line">&lt;html lang=<span class="string">"en"</span>&gt;</span><br><span class="line"></span><br><span class="line">&lt;head&gt;</span><br><span class="line">    &lt;meta charset=<span class="string">"UTF-8"</span>&gt;</span><br><span class="line">    &lt;meta name=<span class="string">"viewport"</span> content=<span class="string">"width=device-width, initial-scale=1.0"</span>&gt;</span><br><span class="line">    &lt;title&gt;取消请求&lt;<span class="regexp">/title&gt;</span></span><br><span class="line"><span class="regexp">&lt;/</span>head&gt;</span><br><span class="line"></span><br><span class="line">&lt;body&gt;</span><br><span class="line">    &lt;div&gt;</span><br><span class="line">        &lt;button onclick=<span class="string">"getOne()"</span>&gt;获取商品列表<span class="number">1</span>&lt;<span class="regexp">/button&gt;</span></span><br><span class="line"><span class="regexp">        &lt;button onclick="getTwo()"&gt;获取商品列表2&lt;/</span>button&gt;</span><br><span class="line">        &lt;button onclick=<span class="string">"cancelReq()"</span>&gt;取消请求&lt;<span class="regexp">/button&gt;</span></span><br><span class="line"><span class="regexp">    &lt;/</span>div&gt;</span><br><span class="line">    &lt;script src=<span class="string">"https://cdn.bootcdn.net/ajax/libs/axios/0.20.0-0/axios.min.js"</span>&gt;&lt;<span class="regexp">/script&gt;</span></span><br><span class="line"><span class="regexp">    &lt;script&gt;</span></span><br><span class="line"><span class="regexp">        let cancel</span></span><br><span class="line"><span class="regexp"></span></span><br><span class="line"><span class="regexp">        function getOne() &#123;</span></span><br><span class="line"><span class="regexp">            if (typeof cancel === 'function') &#123;</span></span><br><span class="line"><span class="regexp">                cancel('强制取消')</span></span><br><span class="line"><span class="regexp">            &#125;</span></span><br><span class="line"><span class="regexp">            axios(&#123;</span></span><br><span class="line"><span class="regexp">                url: 'http:/</span><span class="regexp">/localhost:4000/</span>products1<span class="string">',</span></span><br><span class="line"><span class="string">                cancelToken: new axios.CancelToken(c =&gt; &#123;</span></span><br><span class="line"><span class="string">                    cancel = c</span></span><br><span class="line"><span class="string">                &#125;)</span></span><br><span class="line"><span class="string">            &#125;).then(</span></span><br><span class="line"><span class="string">                response =&gt; &#123;</span></span><br><span class="line"><span class="string">                    cancel = null</span></span><br><span class="line"><span class="string">                    console.log("请求1成功了");</span></span><br><span class="line"><span class="string">                &#125;,</span></span><br><span class="line"><span class="string">                error =&gt; &#123;</span></span><br><span class="line"><span class="string">                    if (axios.isCancel(error)) &#123;</span></span><br><span class="line"><span class="string">                        console.log("取消1导致的错误，不应将cancel置空");</span></span><br><span class="line"><span class="string">                    &#125; else &#123;</span></span><br><span class="line"><span class="string">                        cancel = null</span></span><br><span class="line"><span class="string">                        console.log("请求1失败了");</span></span><br><span class="line"><span class="string">                    &#125;</span></span><br><span class="line"><span class="string">                &#125;</span></span><br><span class="line"><span class="string">            )</span></span><br><span class="line"><span class="string">        &#125;</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">        function getTwo() &#123;</span></span><br><span class="line"><span class="string">            if (typeof cancel === '</span><span class="function"><span class="keyword">function</span>') </span>&#123;</span><br><span class="line">                cancel(<span class="string">'强制取消'</span>)</span><br><span class="line">            &#125;</span><br><span class="line">            axios(&#123;</span><br><span class="line">                url: <span class="string">'http://localhost:4000/products2'</span>,</span><br><span class="line">                cancelToken: <span class="keyword">new</span> axios.CancelToken(<span class="function"><span class="params">c</span> =&gt;</span> &#123;</span><br><span class="line">                    cancel = c</span><br><span class="line">                &#125;)</span><br><span class="line">            &#125;).then(</span><br><span class="line">                response =&gt; &#123;</span><br><span class="line">                    cancel = <span class="literal">null</span></span><br><span class="line">                    <span class="built_in">console</span>.log(<span class="string">"请求2成功了"</span>);</span><br><span class="line">                &#125;,</span><br><span class="line">                error =&gt; &#123;</span><br><span class="line">                    <span class="keyword">if</span> (axios.isCancel(error)) &#123;</span><br><span class="line">                        <span class="built_in">console</span>.log(<span class="string">"取消2导致的错误，不应将cancel置空"</span>);</span><br><span class="line">                    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                        cancel = <span class="literal">null</span></span><br><span class="line">                        <span class="built_in">console</span>.log(<span class="string">"请求2失败了"</span>);</span><br><span class="line">                    &#125;</span><br><span class="line">                &#125;</span><br><span class="line">            )</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="function"><span class="keyword">function</span> <span class="title">cancelReq</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">            <span class="keyword">if</span> (cancel) &#123;</span><br><span class="line">                cancel(<span class="string">'强制取消'</span>)</span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                <span class="built_in">console</span>.log(<span class="string">"没有可以取消的请求"</span>);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &lt;<span class="regexp">/script&gt;</span></span><br><span class="line"><span class="regexp">&lt;/</span>body&gt;</span><br><span class="line"></span><br><span class="line">&lt;<span class="regexp">/html&gt;</span></span><br></pre></td></tr></table></figure>
因为所有请求都是用的一个cancel，所以cancel为function，则证明前一个请求并没有完成，因为完成请求之后cancel会被置为null</li>
</ol>
<p>注意这里要写成上面这样，而不是下面</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">error =&gt; &#123;</span><br><span class="line">                   <span class="keyword">if</span> (axios.isCancel(error)) &#123;</span><br><span class="line">                       <span class="built_in">console</span>.log(<span class="string">"取消1导致的错误，不应将cancel置空"</span>);</span><br><span class="line">                   &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                       cancel = <span class="literal">null</span></span><br><span class="line">                       <span class="built_in">console</span>.log(<span class="string">"请求1失败了"</span>);</span><br><span class="line">                   &#125;</span><br><span class="line">               &#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">error =&gt; &#123;</span><br><span class="line">cancel = <span class="literal">null</span></span><br><span class="line"><span class="built_in">console</span>.log(<span class="string">"请求1失败了"</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>因为error是异步执行的<br>比如：点击发送第一次  —- 点击发送第二次，这时第一次的请求被中断，但是因为error是异步的，所以error的代码并没有马上执行—–点击发送第三次，这是error的代码执行了，cancel被置为null，但此时第二次请求仍在请求中，进入第三次的判断中，cancle不为function，所以无法中断第二次发送的请求，我们使用axios中的isCancel函数可以判断错误是否是因为取消发送请求产生的，若是因为取消发送请求产生的，我们不将cancel置为空就可以解决这个问题</p>
<p>这样虽然完成了要求，但是我们发现重复的地方很多，若是请求多了之后，难道每一个都要再写一遍？有人会想到封装成函数，这里封装成函数并没有改变多少，还是每一个都要调用<br>这就要使用到我上面提到的拦截器了，拦截器可以对所用的请求(响应)做处理，既然所有的请求(响应)里面都有这样相似的代码，用拦截器在请求发送之前(响应得到之后)一起处理，岂不美哉，这就是拦截器的好处，下面我们来看</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br></pre></td><td class="code"><pre><span class="line">&lt;!DOCTYPE html&gt;</span><br><span class="line">&lt;html lang=<span class="string">"en"</span>&gt;</span><br><span class="line"></span><br><span class="line">&lt;head&gt;</span><br><span class="line">    &lt;meta charset=<span class="string">"UTF-8"</span>&gt;</span><br><span class="line">    &lt;meta name=<span class="string">"viewport"</span> content=<span class="string">"width=device-width, initial-scale=1.0"</span>&gt;</span><br><span class="line">    &lt;title&gt;取消请求&lt;<span class="regexp">/title&gt;</span></span><br><span class="line"><span class="regexp">&lt;/</span>head&gt;</span><br><span class="line"></span><br><span class="line">&lt;body&gt;</span><br><span class="line">    &lt;div&gt;</span><br><span class="line">        &lt;button onclick=<span class="string">"getOne()"</span>&gt;获取商品列表<span class="number">1</span>&lt;<span class="regexp">/button&gt;</span></span><br><span class="line"><span class="regexp">        &lt;button onclick="getTwo()"&gt;获取商品列表2&lt;/</span>button&gt;</span><br><span class="line">        &lt;button onclick=<span class="string">"cancelReq()"</span>&gt;取消请求&lt;<span class="regexp">/button&gt;</span></span><br><span class="line"><span class="regexp">    &lt;/</span>div&gt;</span><br><span class="line">    &lt;script src=<span class="string">"https://cdn.bootcdn.net/ajax/libs/axios/0.20.0-0/axios.min.js"</span>&gt;&lt;<span class="regexp">/script&gt;</span></span><br><span class="line"><span class="regexp">    &lt;script&gt;</span></span><br><span class="line"><span class="regexp">        /</span><span class="regexp">/添加请求拦截器</span></span><br><span class="line"><span class="regexp">        axios.interceptors.request.use((config) =&gt; &#123;</span></span><br><span class="line"><span class="regexp">            /</span><span class="regexp">/1.在准备发请求前，取消上一个未完成的请求</span></span><br><span class="line"><span class="regexp">            if (typeof cancel === 'function') &#123;</span></span><br><span class="line"><span class="regexp">                cancel('强制取消')</span></span><br><span class="line"><span class="regexp">            &#125;</span></span><br><span class="line"><span class="regexp">            /</span><span class="regexp">/2.添加一个cancelToken的配置</span></span><br><span class="line"><span class="regexp">            config.cancelToken = new axios.CancelToken(c =&gt; &#123;</span></span><br><span class="line"><span class="regexp">                cancel = c</span></span><br><span class="line"><span class="regexp">            &#125;)</span></span><br><span class="line"><span class="regexp">            return config</span></span><br><span class="line"><span class="regexp">        &#125;)</span></span><br><span class="line"><span class="regexp"></span></span><br><span class="line"><span class="regexp">        /</span><span class="regexp">/添加一个响应拦截器</span></span><br><span class="line"><span class="regexp">        axios.interceptors.response.use(</span></span><br><span class="line"><span class="regexp">            (response) =&gt; &#123;</span></span><br><span class="line"><span class="regexp">                cancel = null</span></span><br><span class="line"><span class="regexp">                return response</span></span><br><span class="line"><span class="regexp">            &#125;,</span></span><br><span class="line"><span class="regexp">            error =&gt; &#123;</span></span><br><span class="line"><span class="regexp">                if (axios.isCancel(error)) &#123;</span></span><br><span class="line"><span class="regexp">                    console.log("取消导致的错误，不应将cancel置空");</span></span><br><span class="line"><span class="regexp">                    /</span><span class="regexp">/中断Promise链</span></span><br><span class="line"><span class="regexp">                    return new Promise(() =&gt; &#123;&#125;)</span></span><br><span class="line"><span class="regexp">                &#125; else &#123;</span></span><br><span class="line"><span class="regexp">                    cancel = null</span></span><br><span class="line"><span class="regexp">                    /</span><span class="regexp">/非取消导致的错误，将错误向下传递 让请求自己处理</span></span><br><span class="line"><span class="regexp">                    return Promise.reject(error)</span></span><br><span class="line"><span class="regexp">                    /</span><span class="regexp">/也可以抛出错误 throw error</span></span><br><span class="line"><span class="regexp">                &#125;</span></span><br><span class="line"><span class="regexp">            &#125;</span></span><br><span class="line"><span class="regexp">        )</span></span><br><span class="line"><span class="regexp"></span></span><br><span class="line"><span class="regexp">        let cancel</span></span><br><span class="line"><span class="regexp"></span></span><br><span class="line"><span class="regexp">        function getOne() &#123;</span></span><br><span class="line"><span class="regexp">            axios(&#123;</span></span><br><span class="line"><span class="regexp">                url: 'http:/</span><span class="regexp">/localhost:4000/</span>products1<span class="string">'</span></span><br><span class="line"><span class="string">            &#125;).then(</span></span><br><span class="line"><span class="string">                response =&gt; &#123;</span></span><br><span class="line"><span class="string">                    console.log("请求1成功了");</span></span><br><span class="line"><span class="string">                &#125;,</span></span><br><span class="line"><span class="string">                error =&gt; &#123;</span></span><br><span class="line"><span class="string">                    //只用处理请求失败的情况，取消请求导致的错误不用处理</span></span><br><span class="line"><span class="string">                    console.log("请求1失败了");</span></span><br><span class="line"><span class="string">                &#125;</span></span><br><span class="line"><span class="string">            )</span></span><br><span class="line"><span class="string">        &#125;</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">        function getTwo() &#123;</span></span><br><span class="line"><span class="string">            axios(&#123;</span></span><br><span class="line"><span class="string">                url: '</span>http:<span class="comment">//localhost:4000/products2'</span></span><br><span class="line">            &#125;).then(</span><br><span class="line">                response =&gt; &#123;</span><br><span class="line">                    <span class="built_in">console</span>.log(<span class="string">"请求2成功了"</span>);</span><br><span class="line">                &#125;,</span><br><span class="line">                error =&gt; &#123;</span><br><span class="line">                    <span class="built_in">console</span>.log(<span class="string">"请求2失败了"</span>);</span><br><span class="line">                &#125;</span><br><span class="line">            )</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="function"><span class="keyword">function</span> <span class="title">cancelReq</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">            <span class="keyword">if</span> (cancel) &#123;</span><br><span class="line">                cancel(<span class="string">'强制取消'</span>)</span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                <span class="built_in">console</span>.log(<span class="string">"没有可以取消的请求"</span>);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &lt;<span class="regexp">/script&gt;</span></span><br><span class="line"><span class="regexp">&lt;/</span>body&gt;</span><br><span class="line"></span><br><span class="line">&lt;<span class="regexp">/html&gt;</span></span><br></pre></td></tr></table></figure>

      
      <!-- reward -->
      
      <div id="reward-btn">
        打赏
      </div>
      
    </div>
    
    
      <!-- copyright -->
      
        <div class="declare">
          <ul class="post-copyright">
            <li>
              <i class="ri-copyright-line"></i>
              <strong>版权声明： </strong s>
              本博客所有文章除特别声明外，均采用 <a href="https://www.apache.org/licenses/LICENSE-2.0.html" rel="external nofollow"
                target="_blank">Apache License 2.0</a> 许可协议。转载请注明出处！
            </li>
          </ul>
        </div>
        
    <footer class="article-footer">
      
          
<div class="share-btn">
      <span class="share-sns share-outer">
        <i class="ri-share-forward-line"></i>
        分享
      </span>
      <div class="share-wrap">
        <i class="arrow"></i>
        <div class="share-icons">
          
          <a class="weibo share-sns" href="javascript:;" data-type="weibo">
            <i class="ri-weibo-fill"></i>
          </a>
          <a class="weixin share-sns wxFab" href="javascript:;" data-type="weixin">
            <i class="ri-wechat-fill"></i>
          </a>
          <a class="qq share-sns" href="javascript:;" data-type="qq">
            <i class="ri-qq-fill"></i>
          </a>
          <a class="douban share-sns" href="javascript:;" data-type="douban">
            <i class="ri-douban-line"></i>
          </a>
          <!-- <a class="qzone share-sns" href="javascript:;" data-type="qzone">
            <i class="icon icon-qzone"></i>
          </a> -->
          
          <a class="facebook share-sns" href="javascript:;" data-type="facebook">
            <i class="ri-facebook-circle-fill"></i>
          </a>
          <a class="twitter share-sns" href="javascript:;" data-type="twitter">
            <i class="ri-twitter-fill"></i>
          </a>
          <a class="google share-sns" href="javascript:;" data-type="google">
            <i class="ri-google-fill"></i>
          </a>
        </div>
      </div>
</div>

<div class="wx-share-modal">
    <a class="modal-close" href="javascript:;"><i class="ri-close-circle-line"></i></a>
    <p>扫一扫，分享到微信</p>
    <div class="wx-qrcode">
      <img src="//api.qrserver.com/v1/create-qr-code/?size=150x150&data=https://lockme.gitee.io/2020/09/07/%E5%AE%9E%E4%BE%8B%E6%BC%94%E7%A4%BAaxios%E6%8B%A6%E6%88%AA%E5%99%A8%E5%92%8C%E5%8F%96%E6%B6%88%E8%AF%B7%E6%B1%82-md/" alt="微信分享二维码">
    </div>
</div>

<div id="share-mask"></div>
      
      
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Node-js/" rel="tag">Node.js</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/axios/" rel="tag">axios</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/%E6%8A%80%E6%9C%AF/" rel="tag">技术</a></li></ul>


    </footer>

  </div>

  
  
  <nav class="article-nav">
    
      <a href="/2020/09/07/axios%E6%8B%A6%E6%88%AA%E5%99%A8%E6%89%A7%E8%A1%8C%E9%A1%BA%E5%BA%8F%E7%9A%84%E6%BA%90%E7%A0%81%E8%A7%A3%E9%87%8A-md/" class="article-nav-link">
        <strong class="article-nav-caption">上一篇</strong>
        <div class="article-nav-title">
          
            axios拦截器执行顺序的源码解释.md
          
        </div>
      </a>
    
    
      <a href="/2020/09/07/HTTP%E8%AF%B7%E6%B1%82%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86%E6%80%BB%E7%BB%93-md/" class="article-nav-link">
        <strong class="article-nav-caption">下一篇</strong>
        <div class="article-nav-title">HTTP请求基础知识总结.md</div>
      </a>
    
  </nav>


  

  
  
<!-- valine评论 -->
<div id="vcomments-box">
    <div id="vcomments">
    </div>
</div>
<script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
<script src='https://cdn.jsdelivr.net/npm/valine@1.3.10/dist/Valine.min.js'></script>
<script>
    new Valine({
        el: '#vcomments',
        app_id: 'pmaQYnbUE9O0H1hiYKLiec4R-gzGzoHsz',
        app_key: 'LMujtI79W4vgEQ3Az5IMo50y',
        path: window.location.pathname,
        notify: 'false',
        verify: 'false',
        avatar: 'mp',
        placeholder: '给我的文章加点评论吧~',
        recordIP: true
    });
    const infoEle = document.querySelector('#vcomments .info');
    if (infoEle && infoEle.childNodes && infoEle.childNodes.length > 0) {
        infoEle.childNodes.forEach(function (item) {
            item.parentNode.removeChild(item);
        });
    }
</script>
<style>
    #vcomments-box {
        padding: 5px 30px;
    }

    @media screen and (max-width: 800px) {
        #vcomments-box {
            padding: 5px 0px;
        }
    }

    #vcomments-box #vcomments {
        background-color: #fff;
    }

    .v .vlist .vcard .vh {
        padding-right: 20px;
    }

    .v .vlist .vcard {
        padding-left: 10px;
    }
</style>

  

  
  
  

</article>
</section>
      <footer class="footer">
  <div class="outer">
    <ul class="list-inline">
      <li>
        &copy;
        2020
        月下ctrlc
      </li>
      <li>
        
        Powered by
        
        
        <a href="https://hexo.io" target="_blank">Hexo</a> Theme <a href="https://github.com/Shen-Yu/hexo-theme-ayer" target="_blank">Ayer</a>
        
      </li>
    </ul>
    <ul class="list-inline">
      <li>
        
        
        <span>
  <i>PV:<span id="busuanzi_value_page_pv"></span></i>
  <i>UV:<span id="busuanzi_value_site_uv"></span></i>
</span>
        
      </li>
      
      <li>
        <!-- cnzz统计 -->
        
        <script type="text/javascript" src='https://s9.cnzz.com/z_stat.php?id=1278069914&amp;web_id=1278069914'></script>
        
      </li>
    </ul>
  </div>
</footer>
      <div class="to_top">
        <div class="totop" id="totop">
  <i class="ri-arrow-up-line"></i>
</div>
      </div>
    </main>
    <aside class="sidebar">
      <button class="navbar-toggle"></button>
<nav class="navbar">
  
  <div class="logo">
    <a href="/"><img src="/images/avatar.jpg" alt="Small Bridge"></a>
  </div>
  
  <ul class="nav nav-main">
    
    <li class="nav-item">
      <a class="nav-item-link" href="/">主页</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/archives">归档</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/categories">分类</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/tags">标签</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/tags/Vue/">Vue</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="http://shenyu-vip.lofter.com" target="_blank" rel="noopener">主题作者相册</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/2020/03/18/about">关于我</a>
    </li>
    
  </ul>
</nav>
<nav class="navbar navbar-bottom">
  <ul class="nav">
    <li class="nav-item">
      
      <a class="nav-item-link nav-item-search"  title="Search">
        <i class="ri-search-line"></i>
      </a>
      
      
      <a class="nav-item-link" target="_blank" href="/atom.xml" title="RSS Feed">
        <i class="ri-rss-line"></i>
      </a>
      
    </li>
  </ul>
</nav>
<div class="search-form-wrap">
  <div class="local-search local-search-plugin">
  <input type="search" id="local-search-input" class="local-search-input" placeholder="Search...">
  <div id="local-search-result" class="local-search-result"></div>
</div>
</div>
    </aside>
    <div id="mask"></div>

<!-- #reward -->
<div id="reward">
  <span class="close"><i class="ri-close-line"></i></span>
  <p class="reward-p"><i class="ri-cup-line"></i>拿钱砸死我吧~</p>
  <div class="reward-box">
    
    <div class="reward-item">
      <img class="reward-img" src="/images/alipay.png">
      <span class="reward-type">支付宝</span>
    </div>
    
    
    <div class="reward-item">
      <img class="reward-img" src="/images/wechat.png">
      <span class="reward-type">微信</span>
    </div>
    
  </div>
</div>
    
<script src="/js/jquery-2.0.3.min.js"></script>


<script src="/js/share.js"></script>


<script src="/js/lazyload.min.js"></script>


<script>
  try {
    var typed = new Typed("#subtitle", {
      strings: ['面朝大海，春暖花开', '愿你一生努力，一生被爱', '想要的都拥有，得不到的都释怀'],
      startDelay: 0,
      typeSpeed: 200,
      loop: true,
      backSpeed: 100,
      showCursor: true
    });
  } catch (err) {
  }

</script>




<script src="/js/tocbot.min.js"></script>

<script>
  // Tocbot_v4.7.0  http://tscanlin.github.io/tocbot/
  tocbot.init({
    tocSelector: '.tocbot',
    contentSelector: '.article-entry',
    headingSelector: 'h1, h2, h3, h4, h5, h6',
    hasInnerContainers: true,
    scrollSmooth: true,
    scrollContainer: 'main',
    positionFixedSelector: '.tocbot',
    positionFixedClass: 'is-position-fixed',
    fixedSidebarOffset: 'auto',
    onClick: (e) => {
      $('.toc-link').removeClass('is-active-link');
      $(`a[href=${e.target.hash}]`).addClass('is-active-link');
      $(e.target.hash).scrollIntoView();
      return false;
    }
  });
</script>



<script src="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.css">
<script src="https://cdn.jsdelivr.net/npm/justifiedGallery@3.7.0/dist/js/jquery.justifiedGallery.min.js"></script>

<script src="/js/ayer.js"></script>



<!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">

    <!-- Background of PhotoSwipe. 
         It's a separate element as animating opacity is faster than rgba(). -->
    <div class="pswp__bg"></div>

    <!-- Slides wrapper with overflow:hidden. -->
    <div class="pswp__scroll-wrap">

        <!-- Container that holds slides. 
            PhotoSwipe keeps only 3 of them in the DOM to save memory.
            Don't modify these 3 pswp__item elements, data is added later on. -->
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>

        <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
        <div class="pswp__ui pswp__ui--hidden">

            <div class="pswp__top-bar">

                <!--  Controls are self-explanatory. Order can be changed. -->

                <div class="pswp__counter"></div>

                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>

                <button class="pswp__button pswp__button--share" style="display:none" title="Share"></button>

                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>

                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>

                <!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
                <!-- element will get class pswp__preloader--active when preloader is running -->
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                        <div class="pswp__preloader__cut">
                            <div class="pswp__preloader__donut"></div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div>
            </div>

            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>

            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>

            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>

        </div>

    </div>

</div>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/default-skin/default-skin.css">
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe-ui-default.min.js"></script>

<script>
    function viewer_init() {
        let pswpElement = document.querySelectorAll('.pswp')[0];
        let $imgArr = document.querySelectorAll(('.article-entry img:not(.reward-img)'))

        $imgArr.forEach(($em, i) => {
            $em.onclick = () => {
                // slider展开状态
                // todo: 这样不好，后面改成状态
                if (document.querySelector('.left-col.show')) return
                let items = []
                $imgArr.forEach(($em2, i2) => {
                    let img = $em2.getAttribute('data-idx', i2)
                    let src = $em2.getAttribute('data-target') || $em2.getAttribute('src')
                    let title = $em2.getAttribute('alt')
                    // 获得原图尺寸
                    const image = new Image()
                    image.src = src
                    items.push({
                        src: src,
                        w: image.width || $em2.width,
                        h: image.height || $em2.height,
                        title: title
                    })
                })
                var gallery = new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, items, {
                    index: parseInt(i)
                });
                gallery.init()
            }
        })
    }
    viewer_init()
</script>


<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
      tex2jax: {
          inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
          processEscapes: true,
          skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
      }
  });

  MathJax.Hub.Queue(function() {
      var all = MathJax.Hub.getAllJax(), i;
      for(i=0; i < all.length; i += 1) {
          all[i].SourceElement().parentNode.className += ' has-jax';
      }
  });
</script>

<script src="https://cdn.jsdelivr.net/npm/mathjax@2.7.6/unpacked/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
<script>
  var ayerConfig = {
    mathjax: true
  }
</script>



<script src="/js/busuanzi-2.3.pure.min.js"></script>



<script type="text/javascript" src="https://js.users.51.la/20544303.js"></script>


    
  </div>
</body>

</html>